package pt.inescid.components.policyengine.heimdall.commons;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class TimeUtilities {

	
	
	public static long parseTimeQty(String thisTime) {

		long timeAsLong = 0;
		
		if (thisTime == null) {
			return (0);
		}
		
		timeAsLong = Integer.parseInt(
				thisTime.substring(0, thisTime.indexOf(" ")));
		String withinUnit = thisTime.substring(
				thisTime.indexOf(" ") + 1);
	
		// SECOND
		timeAsLong *= 1000;
		if (withinUnit.equals("SECOND")) {
			return (timeAsLong);
		}
		
		// MINUTE
		timeAsLong *= 60;
		if (withinUnit.equals("MINUTE")) {
			return (timeAsLong);
		}
		
		// HOUR
		timeAsLong *= 60;
		if (withinUnit.equals("HOUR")) {
			return (timeAsLong);
		}
		
		// DAY
		timeAsLong *= 24;
		if (withinUnit.equals("DAY")) {
			return (timeAsLong);
		}
		
		return (timeAsLong);
	} // parseTimeQty
	
	
	
	
	
	public static long parseCompleteTimeString(String thisTime) {
		GregorianCalendar auxCalendar;
		int hours, minutes, day, month, year;
		int currentIndex = 0, nextIndex = 0;
		
		if ((thisTime == null) ||
				(thisTime.equals("-")) ||
				(thisTime.equals(""))) {
			return (-1);
		}
		
		try {

			// year
			nextIndex = thisTime.indexOf("-");
			if ((nextIndex == -1) ||
					(nextIndex == thisTime.length() - 1)) {
				return (-1);
			}
			else {
				year = Integer.parseInt(thisTime.substring(currentIndex, 
						nextIndex));
			}
			
			// month
			currentIndex = nextIndex + 1;
			nextIndex = thisTime.indexOf("-", currentIndex);
			if ((nextIndex == -1) ||
					(nextIndex == thisTime.length() - 1)) {
				return (-1);
			}
			else {
				month = Integer.parseInt(thisTime.substring(currentIndex, 
						nextIndex));
			}
			
			// day
			currentIndex = nextIndex + 1;
			if (thisTime.indexOf(" ", currentIndex) != -1) {
				nextIndex = thisTime.indexOf(" ", currentIndex);
			}
			else {
				nextIndex = thisTime.length();
			}
			if ((nextIndex - currentIndex) > 2) {
				return (-1);
			}
			else {
				day = Integer.parseInt(thisTime.substring(currentIndex, 
						nextIndex));
			}
			
			// hours
			currentIndex = nextIndex + 1;
			if (currentIndex < thisTime.length()) {
				
				nextIndex = thisTime.indexOf(":", currentIndex);
				if ((nextIndex == -1) ||
						(nextIndex == thisTime.length() - 1)) {
					return (-1);
				}
				else {
					hours = Integer.parseInt(thisTime.substring(currentIndex, 
							nextIndex));
				}
				
				// minutes
				currentIndex = nextIndex + 1;
				nextIndex = thisTime.length();
				minutes = Integer.parseInt(thisTime.substring(currentIndex, 
						nextIndex));
			}
			else {
				hours = 0;
				minutes = 0;
			}
		}
		catch (NumberFormatException e) {
			return (-1);
		}
		
		auxCalendar = new GregorianCalendar();
		auxCalendar.set(Calendar.YEAR, year);
		auxCalendar.set(Calendar.MONTH, month-1);
		auxCalendar.set(Calendar.DAY_OF_MONTH, day);
		auxCalendar.set(Calendar.HOUR_OF_DAY, hours);
		auxCalendar.set(Calendar.MINUTE, minutes);
	
		// Seconds are always zero
		auxCalendar.set(Calendar.SECOND, 0);
		
		// Milliseconds are always zero
		auxCalendar.set(Calendar.MILLISECOND, 0);
		
		return (auxCalendar.getTime().getTime());		

	} // parseCompleteTimeString
	

	public static String getHeaderTimeString(long time) {
		GregorianCalendar auxCalendar;
		String hours, minutes, day, month, year;

		if (time <= 0) {
			return("-");
		}
		else {
			auxCalendar = new GregorianCalendar();
			auxCalendar.setTime(new java.util.Date(time));

			hours = new String("" + auxCalendar.get(Calendar.HOUR_OF_DAY));
			minutes = new String("" + auxCalendar.get(Calendar.MINUTE));
			if (minutes.length() == 1) {
				minutes = new String("0") + minutes;
			};
			day = new String("" + auxCalendar.get(Calendar.DAY_OF_MONTH));
			if (day.length() == 1) {
				day = new String("0") + day;
			};

			month = new String("" + (auxCalendar.get(Calendar.MONTH) + 1));
			if (month.length() == 1) {
				month = new String("0") + month;
			};
			
			year = new String("" + auxCalendar.get(Calendar.YEAR));

			return (day + "." + month +
					"." + year + "&nbsp;|&nbsp;" + hours + ":" + minutes);
		}
	};

	
	public static String now() {
		return (getFormattedTimeString(
				new GregorianCalendar().getTime().getTime()));
	} // now
	
	
	public static String beginOfDay() {
		GregorianCalendar auxCalendar;
		auxCalendar = new GregorianCalendar();

		auxCalendar.set(Calendar.HOUR_OF_DAY, 0);
		auxCalendar.set(Calendar.MINUTE, 0);
		
		return (getFormattedTimeString(
				auxCalendar.getTime().getTime()));
	} // now
	
	
	
	public static String getFormattedTimeString(long time) {
		GregorianCalendar auxCalendar;
		String day, month, year;

		if (time <= 0) {
			return("");
		}
		else {
			auxCalendar = new GregorianCalendar();
			auxCalendar.setTime(new java.util.Date(time));

			day = new String("" + auxCalendar.get(Calendar.DAY_OF_MONTH));
			if (day.length() == 1) {
				day = new String("0") + day;
			};
			month = new String("" + (auxCalendar.get(Calendar.MONTH) + 1));
			if (month.length() == 1) {
				month = new String("0") + month;
			};
			year = new String("" + auxCalendar.get(Calendar.YEAR));

			return (year + "-" + month + "-" + day);
		}
	};

	public static String getFormattedHourString(long time) {
		GregorianCalendar auxCalendar;
		String hour, minute;

		if (time <= 0) {
			return("-");
		}
		else {
			auxCalendar = new GregorianCalendar();
			auxCalendar.setTime(new java.util.Date(time));

			hour = new String("" + auxCalendar.get(Calendar.HOUR_OF_DAY));
			minute = new String("" + auxCalendar.get(Calendar.MINUTE));
			if (minute.length() == 1) {
				minute = new String("0") + minute;
			};

			return (hour + ":" + minute);
		}
	};

	public static String getCompleteFormattedDateString(long time) {
		GregorianCalendar auxCalendar;
		String hours, minutes, day, month, year;

		if (time <= 0) {
			return("-");
		}
		else {
			auxCalendar = new GregorianCalendar();
			auxCalendar.setTime(new java.util.Date(time));

			hours = new String("" + auxCalendar.get(Calendar.HOUR_OF_DAY));
			minutes = new String("" + auxCalendar.get(Calendar.MINUTE));
			if (minutes.length() == 1) {
				minutes = new String("0") + minutes;
			};
			day = new String("" + auxCalendar.get(Calendar.DAY_OF_MONTH));
			if (day.length() == 1) {
				day = new String("0") + day;
			};
			month = new String("" + (auxCalendar.get(Calendar.MONTH) + 1));
			if (month.length() == 1) {
				month = new String("0") + month;
			};
			year = new String("" + auxCalendar.get(Calendar.YEAR));

			return (year + "-" + month + "-" + day + " " + hours + ":" + minutes);
		}
	};

	public static String getSimpleTimeString(long time) {
		GregorianCalendar auxCalendar;
		String hours, minutes;

		if (time <= 0) {
			return("-");
		}
		else {
			auxCalendar = new GregorianCalendar();
			auxCalendar.setTime(new java.util.Date(time));

			hours = new String("" + auxCalendar.get(Calendar.HOUR_OF_DAY));
			minutes = new String("" + auxCalendar.get(Calendar.MINUTE));
			if (minutes.length() == 1) {
				minutes = new String("0") + minutes;
			};

			return (hours + ":" + minutes);
		}
	};

	public static String getFormattedBeginCurrentYear() {
		GregorianCalendar auxDate = 
			new GregorianCalendar();
		
		auxDate.set(Calendar.MONTH, 0);
		auxDate.set(Calendar.DAY_OF_MONTH, 1);
		auxDate.set(Calendar.HOUR_OF_DAY, 0);
		auxDate.set(Calendar.MINUTE, 0);
		auxDate.set(Calendar.SECOND, 0);
		auxDate.set(Calendar.MILLISECOND, 0);
		
		return (getFormattedTimeString(auxDate.getTime().getTime()));
	};
		
	public static String getFormattedEndCurrentYear() {
		GregorianCalendar auxDate = 
			new GregorianCalendar();
		
		auxDate.set(Calendar.MONTH, 11);
		auxDate.set(Calendar.DAY_OF_MONTH, 31);
		auxDate.set(Calendar.HOUR_OF_DAY, 23);
		auxDate.set(Calendar.MINUTE, 59);
		auxDate.set(Calendar.SECOND, 59);
		auxDate.set(Calendar.MILLISECOND, 999);
		
		return (getFormattedTimeString(auxDate.getTime().getTime()));
	};
		
	public static String getFormattedBeginCurrentMonth() {
		GregorianCalendar auxDate = 
			new GregorianCalendar();
		
		auxDate.set(Calendar.DAY_OF_MONTH, 1);
		auxDate.set(Calendar.HOUR_OF_DAY, 0);
		auxDate.set(Calendar.MINUTE, 0);
		auxDate.set(Calendar.SECOND, 0);
		auxDate.set(Calendar.MILLISECOND, 0);
		
		return (getFormattedTimeString(auxDate.getTime().getTime()));
	};
		
	public static String getFormattedEndCurrentMonth() {
		GregorianCalendar auxDate = 
			new GregorianCalendar();
		
		auxDate.set(Calendar.DAY_OF_MONTH, 
				auxDate.getActualMaximum(Calendar.DAY_OF_MONTH));
		auxDate.set(Calendar.HOUR_OF_DAY, 23);
		auxDate.set(Calendar.MINUTE, 59);
		auxDate.set(Calendar.SECOND, 59);
		auxDate.set(Calendar.MILLISECOND, 999);
		
		return (getFormattedTimeString(auxDate.getTime().getTime()));
	};
		
	public static String getCompleteFormattedTimeString(long time) {
		GregorianCalendar auxCalendar;
		String hours, minutes, seconds, day, month, year;

		if (time == -1) {
			return("-");
		}
		else {
			auxCalendar = new GregorianCalendar();
			auxCalendar.setTime(new java.util.Date(time));

			hours = new String("" + auxCalendar.get(Calendar.HOUR_OF_DAY));
			minutes = new String("" + auxCalendar.get(Calendar.MINUTE));
			seconds = new String("" + auxCalendar.get(Calendar.SECOND));
			if (hours.length() == 1) {
				hours = new String("0") + hours;
			};
			if (minutes.length() == 1) {
				minutes = new String("0") + minutes;
			};
			if (seconds.length() == 1) {
				seconds = new String("0") + seconds;
			};
			day = new String("" + auxCalendar.get(Calendar.DAY_OF_MONTH));
			if (day.length() == 1) {
				day = new String("0") + day;
			};
			month = new String("" + (auxCalendar.get(Calendar.MONTH) + 1));
			if (month.length() == 1) {
				month = new String("0") + month;
			};
			year = new String("" + auxCalendar.get(Calendar.YEAR));

			return (year + "-" + month + "-" + day + " " + hours + "_" + 
					minutes + "_" + seconds);
		}
	};

	
	public static String getSQLFormattedTimeString(long time) {
		GregorianCalendar auxCalendar;
		String day, month, year;

		if (time == -1) {
			return("-");
		}
		else {
			auxCalendar = new GregorianCalendar();
			auxCalendar.setTime(new java.util.Date(time));

			day = new String("" + auxCalendar.get(Calendar.DAY_OF_MONTH));
			if (day.length() == 1) {
				day = new String("0") + day;
			};
			month = new String("" + (auxCalendar.get(Calendar.MONTH) + 1));
			if (month.length() == 1) {
				month = new String("0") + month;
			};
			year = new String("" + auxCalendar.get(Calendar.YEAR));

			return (year + month + day);
		}
	};

	public static String getCompleteSQLFormattedDateString(long time) {
		GregorianCalendar auxCalendar;
		String hours, minutes, day, month, year;

		if (time == -1) {
			return("-");
		}
		else {
			auxCalendar = new GregorianCalendar();
			auxCalendar.setTime(new java.util.Date(time));

			hours = new String("" + auxCalendar.get(Calendar.HOUR_OF_DAY));
			minutes = new String("" + auxCalendar.get(Calendar.MINUTE));
			if (minutes.length() == 1) {
				minutes = new String("0") + minutes;
			};
			day = new String("" + auxCalendar.get(Calendar.DAY_OF_MONTH));
			if (day.length() == 1) {
				day = new String("0") + day;
			};
			month = new String("" + (auxCalendar.get(Calendar.MONTH) + 1));
			if (month.length() == 1) {
				month = new String("0") + month;
			};
			year = new String("" + auxCalendar.get(Calendar.YEAR));

			return (year + "-" + month + "-" + day + " " + hours + ":" + minutes);
		}
	};
	
	
	public static GregorianCalendar getBeginOfTheDay() {
		GregorianCalendar now = new GregorianCalendar();
//		now.add(GregorianCalendar.DAY_OF_MONTH, 1);
		now.set(Calendar.HOUR_OF_DAY, 0);
		now.set(Calendar.MINUTE, 0);
		now.set(Calendar.SECOND, 0);
		now.set(Calendar.MILLISECOND, 0);
		
		return (now);
	} // getBeginOfTheDay
	
	
	public static String getFormattedCurrentTime() {
		GregorianCalendar now = new GregorianCalendar();
		
		return (getFormattedTimeString(now.getTime().getTime()));
	} // getFormattedCurrentTime
	
	public static String getFormattedCurrentHour() {
		GregorianCalendar now = new GregorianCalendar();
		
		return (getFormattedHourString(now.getTime().getTime()));
	} // getFormattedCurrentTime
	
	public static long getCurrentTime() {
		GregorianCalendar now = new GregorianCalendar();
		
		return (now.getTime().getTime());
	} // getCurrentTime
	
	
	public static String getCompleteFormattedCurrentTime() {
		GregorianCalendar now = new GregorianCalendar();
		
		return (getCompleteFormattedTimeString(now.getTime().getTime()));
	} // getFormattedCurrentTime
	
	
	public static boolean isExpired(long expirationDate) {
		long now = new GregorianCalendar().getTime().getTime();
		
		if (now > expirationDate) {
			return (true);
		}
		else {
			return (false);
		}
	} // isExpired
	
	
	public static boolean sameDay(long firstDate, long SecondDate) {
		GregorianCalendar auxFirstDate = new GregorianCalendar();
		GregorianCalendar auxSecondDate = new GregorianCalendar();
		
		auxFirstDate.setTime(new java.util.Date(firstDate));
		auxSecondDate.setTime(new java.util.Date(SecondDate));
		
		if ((auxFirstDate.get(Calendar.YEAR) == 
					auxSecondDate.get(Calendar.YEAR)) &&
					(auxFirstDate.get(Calendar.MONTH) == 
						auxSecondDate.get(Calendar.MONTH)) &&
						(auxFirstDate.get(Calendar.DAY_OF_MONTH) == 
							auxSecondDate.get(Calendar.DAY_OF_MONTH))) {
			return (true);
		}
		else {
			return (false);
		}
	} // sameDay
	
	
	public static boolean sameMinute(long firstDate, long secondDate) {
		GregorianCalendar auxFirstDate = new GregorianCalendar();
		GregorianCalendar auxSecondDate = new GregorianCalendar();
		
		auxFirstDate.setTime(new java.util.Date(firstDate));
		auxSecondDate.setTime(new java.util.Date(secondDate));
		
		if (sameDay(firstDate, secondDate) && 
				(auxFirstDate.get(Calendar.HOUR_OF_DAY) == 
					auxSecondDate.get(Calendar.HOUR_OF_DAY)) &&
					(auxFirstDate.get(Calendar.MINUTE) == 
						auxSecondDate.get(Calendar.MINUTE))) {
			return (true);
		}
		else {
			return (false);
		}
	} // sameMinute
	
};
